<?php
include_once('admin-load.php');
$where = '';
$tn = '';
if (isset($_GET['tn']) && $_GET['tn'] != '') {
    $tn = $dbna->escape($_GET['tn']);
    $where = " AND tr.bill_id =  $tn";
}
$query = "SELECT  tr.id, tr.destination, tr.bill_id, tr.delivered_date, tr.weight, ts.status, lo.location
    FROM tracking tr, tracking_status ts, location lo WHERE tr.tracking_status_id = ts.id AND tr.location_id = lo.id $where";
$tracking = $dbna->get_results($query);

//print_r($tracking)
if (isset($_GET['action']) && $_GET['action'] == 'delete') {
    $tracking_id = $_GET['tracking_id'];
    if (isset($_GET['confirm']) && $_GET['confirm'] == 1) {
        delete_tracking($tracking_id);
    }
    $msg = 'Do you want to delete tracking? <a href="tracking.php?action=delete&tracking_id=' . $tracking_id . '&confirm=1">YES</a> <a href="tracking.php">NO</a>';
}
?>
<?php
include_once 'header.php';
?>
<div id="main_content">
    <div class="container">
        <h5>Tracking Form</h5>
        <form action="tracking.php" method="get" class="form-search">
            <div>
                <label for="tracking_number">Tracking Number</label>
                <input type="text" name="tn" value="<?php echo $tn ?>" id="tracking_number" />
                <input type="submit" value="Filter"/>
            </div>
        </form>
    </div>
    <div id="main">
        <h2>Tracking</h2>
        <?php
        if (isset($msg)) {
            // echo '<div class="warning">'. $msg . '</div>';
        }
        ?>
        <table>
            <thead>
                <tr>
                    <th>Tracking Number</th>
                    <th>Destination</th>
                    <th>Delivered</th>
                    <th>Status</th>
                    <th>Location</th>
                    <th>Weight</th>
                    <th>View/Edit/Delete</th>
                </tr>
            </thead>
            <tbody>
                <?php
                if (!empty($tracking)) {
                    foreach ($tracking as $t) {
                        $views = '<a href="tracking-view.php?tracking_id=' . $t->id . '">View</a>';
                        $edit = '<a href="tracing.php?tracking_id=' . $t->id . '"> Edit</a>';
                        $delete = '<a href="tracking.php?action=delete&tracking_id=' . $t->id . '">Delete</a>';
                        
                        
                        $action = $views .' '. $edit . ' ' . $delete;
                       /* printf('<tr><td>%t</td><td>%t</td><td>%t</td><td>%t</td><td>%t</td><td>%t</td><td>%t</td><td>%t</td><td>%t</td><td>%t</td><td>%t</td></tr>', 
                                $t->bill_id, $t->shipped_on, $t->destination, $t->delivered_date, $t->description, $t->detail_Date, $t->location_id, $t->sign_by, $t->weight, $t->piece, $edit. ' '.$delete
                        );*/
                       ?>
                <tr>
                    <td><?php echo $t->bill_id ?></td>
                    <td><?php echo $t->destination ?></td>
                    <td><?php echo $t->delivered_date ?></td>
                    <td><?php echo $t->status ?></td>
                    <td><?php echo $t->location ?></td>
                    <td><?php echo $t->weight ?></td>
                    <td><?php echo$action ?></td>
                </tr>
                       <?php
                    }
                }
                ?>
            </tbody>
        </table>
    </div>
</div>
<?php
include_once 'footer.php';
?>